import { createRouter, createWebHistory } from 'vue-router';
import { showNotify } from 'vant'
import 'vant/es/notify/style';
import CarVue from '@/page/Car.vue';


const router = createRouter({
    history: createWebHistory('/sakura'),
    routes: [
        {
            path: '/',
            component: () => import('@/page/Main.vue'),
            children: [
                {
                    path: 'home',
                    alias: '/',
                    component: () => import('@/page/Home.vue')
                },
                {
                    path: 'self',
                    component: () => import('@/page/Self.vue'),
                    beforeEnter() {
                        const token = localStorage.getItem('token')
                        const userid = localStorage.getItem('userid')
                        if (!token || !userid) {
                            showNotify({ type: 'danger', message: '未登录' })
                            return '/login'
                        }
                    }
                },
                {
                    path: 'car',
                    component: CarVue,
                    beforeEnter() {
                        const token = localStorage.getItem('token')
                        const userid = localStorage.getItem('userid')
                        if (!token || !userid) {
                            showNotify({ type: 'danger', message: '未登录' })
                            return '/login'
                        }
                    }
                }
            ]
        },

        {
            path: '/login',
            component: () => import('@/page/Login.vue'),
            beforeEnter() {
                const token = localStorage.getItem('token')
                const userid = localStorage.getItem('userid')
                if (token && userid) {
                    return '/'
                }
            }
        },
        {
            path: '/search',
            component: () => import('@/page/Search.vue')
        },
        {
            path: '/register',
            component: () => import('@/page/Register.vue')
        },
        {
            path: '/cities',
            component: () => import('@/page/Cities.vue')
        },
        {
            path: '/product/:id',
            component: () => import('@/page/Product.vue')
        },
        {
            path: '/:path(.*)',
            component: () => import('@/page/404.vue')
        }
    ]
})

router.beforeEach((to, from) => {

})
export default router